package code.cando;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;

/**
 * author : yeswater
 * date : 2023/12/8
 */

public class T046 {
    static List<List<Integer>> res = new ArrayList<>();
    static LinkedList<Integer> path = new LinkedList<>();
    static HashSet set = new HashSet();

    public static void main(String[] args) {
        int[] nums = {1, 2, 3};
        System.out.println(permute(nums).toString());
    }

    public static List<List<Integer>> permute(int[] nums) {
        if (nums.length < 1) {
            return res;
        }
        func(nums, 0);
        return res;
    }

    private static void func(int[] nums, int index) {
        if (path.size() == nums.length) {
            res.add(new ArrayList<>(path));
            return;
        }

        for (int i = index; i < nums.length; i++) {
            if (set.contains(nums[i])) {
                continue;
            }
            path.add(nums[i]);
            set.add(nums[i]);
            func(nums, 0);
            path.removeLast();
            set.remove(nums[i]);
        }
    }
}
